package com.yxzr.youyou;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.yxzr.youyou.base.BaseActivity;
import com.yxzr.youyou.retrofit.BaseObserver;
import com.yxzr.youyou.retrofit.RequestUtil;
import com.yxzr.youyou.utils.NullStringToEmptyAdapterFactory;
import com.yxzr.youyou.view.CountDownButton;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class RegisterActivity extends BaseActivity {
    private EditText register_et_recode,register_et_phone,register_et_code,register_et_nickname,register_et_pwd,register_et_surepwd;
    private CountDownButton cd_btn;
    private Button register_btn;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        setTitleWithBack();
        initView();
    }

    private void initView() {
        register_et_recode = findViewById(R.id.register_et_recode);
        register_et_phone = findViewById(R.id.register_et_phone);
        register_et_code = findViewById(R.id.register_et_code);
        register_et_nickname = findViewById(R.id.register_et_nickname);
        register_et_pwd = findViewById(R.id.register_et_pwd);
        register_et_surepwd = findViewById(R.id.register_et_surepwd);
        cd_btn = findViewById(R.id.cd_btn);
        register_btn = findViewById(R.id.register_btn);

        cd_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.isEmpty(register_et_phone.getText().toString().trim())) {
                    Toast.makeText(RegisterActivity.this, "请输入手机号码", Toast.LENGTH_SHORT).show();
                }else {
                    getCode();
                }
            }
        });
        register_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                register();
            }
        });
    }
    private void getCode(){
        cd_btn.startCD();
        RequestUtil.getInstence().API().sendCode(register_et_phone.getText().toString(),"1")
                .subscribeOn(Schedulers.io()) // 在子线程中进行Http访问
                .observeOn(AndroidSchedulers.mainThread()) // UI线程处理返回接口
                .subscribe(new BaseObserver(this,true) {
                    @Override
                    protected void onSuccees(String t) {
                        Toast.makeText(RegisterActivity.this, "发送成功", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    protected void onFail(int error_code) {
                        cd_btn.removeCountDown();
                    }

                    @Override
                    protected void onComp() {

                    }

                    @Override
                    protected void onErr(Throwable e) {
                        cd_btn.removeCountDown();
                        Toast.makeText(RegisterActivity.this, "获取验证码失败", Toast.LENGTH_SHORT).show();
                    }
                });
    }
    private void register(){
        if ("".equals(register_et_phone.getText().toString().trim())){
            Toast.makeText(RegisterActivity.this, "请输入手机号", Toast.LENGTH_SHORT).show();
            return;
        }
        if ("".equals(register_et_code.getText().toString().trim())){
            Toast.makeText(RegisterActivity.this, "请输入验证码", Toast.LENGTH_SHORT).show();
            return;
        }
        if ("".equals(register_et_nickname.getText().toString().trim())){
            Toast.makeText(RegisterActivity.this, "请输入昵称", Toast.LENGTH_SHORT).show();
            return;
        }
        if ("".equals(register_et_pwd.getText().toString().trim())){
            Toast.makeText(RegisterActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
            return;
        }
        if ("".equals(register_et_surepwd.getText().toString().trim())){
            Toast.makeText(RegisterActivity.this, "请重复密码", Toast.LENGTH_SHORT).show();
            return;
        }
        if (!register_et_pwd.getText().toString().trim().equals(register_et_surepwd.getText().toString().trim())){
            Toast.makeText(RegisterActivity.this, "两次密码输入不一致", Toast.LENGTH_SHORT).show();
            return;
        }
        RequestUtil.getInstence().API().register(register_et_phone.getText().toString().trim(),register_et_pwd.getText().toString().trim(),
                register_et_recode.getText().toString().trim(),register_et_code.getText().toString().trim(),
                register_et_nickname.getText().toString().trim(),register_et_surepwd.getText().toString().trim())
                .subscribeOn(Schedulers.io()) // 在子线程中进行Http访问
                .observeOn(AndroidSchedulers.mainThread()) // UI线程处理返回接口
                .subscribe(new BaseObserver(RegisterActivity.this,true){
                    @Override
                    protected void onSuccees(String t) {

                        Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                        finish();
                    }

                    @Override
                    protected void onFail(int error_code) {

                    }

                    @Override
                    protected void onComp() {

                    }

                    @Override
                    protected void onErr(Throwable e) {


                    }
                });

    }
}
